<template>
  <div>
    <el-form label-width="100px" :inline="true">
      <el-form-item label="姓名/联系方式">
        <el-input
          v-model="search.condition"
          placeholder="请输入姓名/联系方式"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="所属部门">
        <el-cascader
          :options="departmentList"
          v-model="search.department_id"
          :props="{ checkStrictly: true }"
          :show-all-levels="false"
          placeholder="请选择所属部门"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="岗位">
        <el-cascader
          :options="positionList"
          v-model="search.position_id"
          :props="{ checkStrictly: true }"
          :show-all-levels="false"
          placeholder="请选择岗位"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="状态">
        <nice-select
          v-model="search.status"
          placeholder="请选择状态"
          clearable
          :options="statusList"
        ></nice-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" size="small"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <nice-table
      :data="list"
      :showIndex="true"
      :columns="columns"
      :pagination="pagination"
    ></nice-table>
  </div>
</template>
<script>
import base from '@/views/base.vue'
import { manageTree } from '@/api/api'
import { getStaffList, findEnumTreeByTableNameAndFieldName } from '@/api/user'
import { myEnumsMixins } from '@/utils/mixins'
export default {
  name: 'staff-administration',
  extends: base,
  mixins: [myEnumsMixins],
  data () {
    return {
      search: {
        condition: '',
        department_id: [],
        position_id: '',
        status: ''
      },
      statusList: [
        { label: '在职', value: 0 },
        { label: '离职', value: 1 }
      ],
      departmentList: [],
      positionList: [],
      list: [],
      pagination: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
        handleCurrentChange: this.handleTeacherCurrentChange,
        handleSizeChange: this.handleTeacherSizeChange
      },
      columns: [
        {
          label: '头像',
          prop: 'head_url',
          isImg: true
        },
        {
          label: '姓名/昵称',
          prop: 'real_nick',
          extends: [
            {
              clickFunction: val => {
                this.viewDetail(val)
              }
            }
          ]
        },
        {
          label: '性别/年龄',
          prop: 'sex_birth'
        },
        {
          label: '联系方式',
          prop: 'phone',
          width: 150,
          extends: [
            {
              clickFunction: val => {
                this.viewPhone(val)
              },
              isView: true
            }
          ]
        },
        {
          label: '所属部门',
          prop: 'department_name'
        },
        {
          label: '岗位',
          prop: 'position_id'
        },
        {
          label: '讲师评级',
          prop: 'speaker_rating',
          extends: [
            {
              filterFunction: val => {
                return this.$filterEnums(
                  val,
                  this.selectEnums.nice_teacher_info.speaker_rating
                )
              }
            }
          ]
        },
        {
          label: '主讲年级/科目',
          prop: 'grade_subject'
        },
        {
          label: '教学经验',
          prop: 'year_of_work'
        },
        {
          label: '状态',
          prop: 'status',
          extends: [
            {
              filterFunction: val => {
                return this.$filterEnums(val, this.statusList)
              }
            }
          ]
        }
      ]
    }
  },
  activated () {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList () {
      let res = await manageTree({
        tableName: 'nice_organization'
      })
      if (res[0]) {
        return
      }
      let data = res[1]
      if (data && data.code === 0) {
        let list = []
        if (data.data) {
          list.push({
            label: data.data.Name,
            value: data.data.id,
            parent: data.data.Parent
          })
          if (data.data.child && data.data.child.length) {
            list[0].children = []
            this.departmentList = [this.queryList(data.data.child, list[0])]
          }
        }
        this.getPositionList()
      }
    },
    queryList (tdata, list) {
      tdata.forEach((ele, index) => {
        list.children.push({
          label: ele.Name,
          value: ele.id,
          parent: ele.Parent
        })
        if (ele.child && ele.child.length) {
          list.children[index].children = []
          this.queryList(ele.child, list.children[index])
        }
      })
      return list
    },
    async getPositionList () {
      let res = await findEnumTreeByTableNameAndFieldName({
        tableName: 'nice_inner_user',
        fieldName: 'position_id'
      })
      if (res[0]) {
      }
      let data = res[1]
      if (data && data.code === 0) {
        let array = [data.data]
        let response = []
        this.positionsQueryList(array, response)
        this.positionList = this.$deepcopy(response)
        this.getDataList()
      } else {
        this.$message.error(data.msg)
      }
    },
    positionsQueryList (tdata, list) {
      if (tdata.length) {
        tdata.forEach((ele, index) => {
          list[index] = {
            label: ele.display_value,
            value: ele.t_id,
            parent: ele.parent
          }
          let arr = []
          this.positionsQueryList(ele.child, arr)
          if (arr.length) {
            list[index].children = arr
          }
        })
      }
    },
    async getDataList () {
      let res = await getStaffList({
        'nice_user_base.status': this.search.status,
        'nice_user_base.department_id': this.search.department_id.length
          ? this.search.department_id[this.search.department_id.length - 1]
          : '',
        'nice_user_base.position_id': this.search.position_id.length
          ? this.search.position_id[this.search.position_id.length - 1]
          : '',
        'nice_user_base.condition': this.search.condition,
        'page.currPage': this.pagination.pageNum,
        'page.pageSize': this.pagination.pageSize
      })
      if (res[0]) {
        return
      }
      let data = res[1]
      if (data && data.code === 0) {
        if (data.data.pageList) {
          this.pagination.total = data.data.pageList.totalCount
          this.list = data.data.pageList.list
          if (this.list && this.list.length) {
            this.list.forEach(ele => {
              ele.isView = false
              ele.real_nick = ele.nick_name
                ? ele.real_name + '/' + ele.nick_name
                : ele.real_name
              ele.sex = ele.sex === 0 ? '男' : ele.sex === 1 ? '女' : ''
              let age = ele.birth_date
                ? this.$age(this.$formatDate(ele.birth_date, 'yyyy-MM-dd'))
                : ''
              if (ele.sex && ele.birth_date) {
                ele.sex_birth = ele.sex + '/' + age
              } else if (
                (ele.sex && !ele.birth_date) ||
                (!ele.sex && ele.birth_date)
              ) {
                ele.sex_birth = ele.sex ? ele.sex : age
              } else {
                ele.sex_birth = ''
              }
              if (ele.speaker_grade && ele.speaker_subject) {
                ele.grade_subject =
                  this.$filterEnums(
                    ele.speaker_grade,
                    this.selectEnums.nice_teacher_info.speaker_grade
                  ) +
                  '/' +
                  this.$filterEnums(
                    ele.speaker_subject,
                    this.selectEnums.nice_teacher_info.speaker_subject
                  )
              } else if (
                (ele.speaker_grade && !ele.speaker_subject) ||
                (!ele.speaker_grade && ele.speaker_subject)
              ) {
                ele.grade_subject = ele.speaker_grade
                  ? this.$filterEnums(
                      ele.speaker_grade,
                      this.selectEnums.nice_teacher_info.speaker_grade
                    )
                  : this.$filterEnums(
                      ele.speaker_subject,
                      this.selectEnums.nice_teacher_info.speaker_subject
                    )
              } else {
                ele.grade_subject = ''
              }
              ele.phone = ele.mobile
              ele.position_id = this.$filterTree(
                ele.position_id,
                this.positionList
              )
              ele.start_work_date = ele.start_work_date || ''
              ele.speaker_rating = ele.speaker_rating || ''
            })
          }
        }
      }
    },
    onSubmit () {
      this.pagination.pageNum = 1
      this.getDataList()
    },
    viewPhone (row) {
      this.list.forEach(ele => {
        if (ele.id !== row.id) {
          ele.isView = false
        }
      })
      row.isView = !row.isView
      this.list = [...this.list]
    },
    viewDetail (row) {
      this.$router.push({
        name: 'staff-detail',
        params: { id: row.id }
      })
    },
    handleTeacherCurrentChange (val) {
      this.pagination.pageNum = val
      this.getDataList()
    },
    handleTeacherSizeChange (val) {
      this.pagination.pageSize = val
      this.getDataList()
    }
  }
}
</script>
<style scoped></style>
